package org.jeecg.basic.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aliyuncs.utils.StringUtils;
import org.apache.poi.util.StringUtil;
import org.jeecg.basic.entity.MesLineCfg;
import org.jeecg.basic.service.IMesLineCfgService;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.basic.entity.MesPartTagCfg;
import org.jeecg.basic.service.IMesPartTagCfgService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 自定义参数
 * @Author: LiCY
 * @Date:   2023-08-06
 * @Version: V1.0
 */
@Api(tags="自定义参数")
@RestController
@RequestMapping("/basic/mesPartTagCfg")
@Slf4j
public class MesPartTagCfgController extends JeecgController<MesPartTagCfg, IMesPartTagCfgService> {
	@Autowired
	private IMesPartTagCfgService mesPartTagCfgService;
	@Autowired
	private CommonAPI commonAPI;
	 @Autowired
	 private IMesLineCfgService  mesLineCfgService;
	
	/**
	 * 分页列表查询666
	 *
	 * @param mesPartTagCfg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "自定义参数-分页列表查询")
	@ApiOperation(value="自定义参数-分页列表查询", notes="自定义参数-分页列表查询")
	@GetMapping(value = "/list")
	@DS("#header.his")
	public Result<IPage<MesPartTagCfg>> queryPageList(MesPartTagCfg mesPartTagCfg,
								   String partNo,
									String pnGroup,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MesPartTagCfg> queryWrapper = QueryGenerator.initQueryWrapper(mesPartTagCfg, req.getParameterMap());
		queryWrapper.in("partno",partNo,pnGroup);
		Page<MesPartTagCfg> page = new Page<MesPartTagCfg>(pageNo, pageSize);
		IPage<MesPartTagCfg> pageList = mesPartTagCfgService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param mesPartTagCfg
	 * @return
	 */
	@AutoLog(value = "自定义参数-添加")
	@ApiOperation(value="自定义参数-添加", notes="自定义参数-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_part_tag_cfg:add")
	@PostMapping(value = "/add")
	@DS("#header.line")
	public Result<String> add(@RequestBody MesPartTagCfg mesPartTagCfg) {
		Integer counterValue = commonAPI.getCounterValue("recid.mes_part_tag_cfg","0");
		mesPartTagCfg.setRecId(counterValue);
		isFsn(mesPartTagCfg);
		mesPartTagCfgService.save(mesPartTagCfg);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param mesPartTagCfg
	 * @return
	 */
	@AutoLog(value = "自定义参数-编辑")
	@ApiOperation(value="自定义参数-编辑", notes="自定义参数-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_part_tag_cfg:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	@DS("#header.line")
	public Result<String> edit(@RequestBody MesPartTagCfg mesPartTagCfg) {
		isFsn(mesPartTagCfg);
		mesPartTagCfgService.updateById(mesPartTagCfg);
		return Result.OK("编辑成功!");
	}

	 private void isFsn(MesPartTagCfg mesPartTagCfg) {
		 if (mesPartTagCfg.getName().equals("fsn")){
			 //获取规则
			 String value = mesPartTagCfg.getValue();
			 String str = "";
			 //获取产线编号
			 List<MesLineCfg> list = mesLineCfgService.list();
			 MesLineCfg mesLineCfg = list.get(0);
			 if (value.equals("1")){
				 str = mesLineCfg.getCode()+"[Y][M1][D][SID,"+mesLineCfg.getLine()+"_FSN,1,4]";
			 }else{
				 str = "[StnData.Wo.WoId][YY][MM][DD][SID,"+mesLineCfg.getLine()+"_FSN,1,4]";
			 }
			 mesPartTagCfg.setValue(str);
		 }
	 }

	 /**
	 *   通过选中对象删除
	 *
	 * @param mesPartTagCfg
	 * @return
	 */
	@AutoLog(value = "自定义参数-通过id删除")
	@ApiOperation(value="自定义参数-通过id删除", notes="自定义参数-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_part_tag_cfg:delete")
	@RequestMapping(value = "/delete")
	public Result<String> delete(@RequestBody MesPartTagCfg mesPartTagCfg) {
		mesPartTagCfgService.removeById(mesPartTagCfg);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param list
	 * @return
	 */
	@AutoLog(value = "自定义参数-批量删除")
	@ApiOperation(value="自定义参数-批量删除", notes="自定义参数-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_part_tag_cfg:deleteBatch")
	@RequestMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestBody List<MesPartTagCfg> list) {
	    list.forEach(item->{
    			mesPartTagCfgService.removeById(item);
    		});
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "自定义参数-通过id查询")
	@ApiOperation(value="自定义参数-通过id查询", notes="自定义参数-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("#header.his")
	public Result<MesPartTagCfg> queryById(@RequestParam(name="id",required=true) String id) {
		MesPartTagCfg mesPartTagCfg = mesPartTagCfgService.getById(id);
		if(mesPartTagCfg==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mesPartTagCfg);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mesPartTagCfg
    */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_part_tag_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("#header.his")
    public ModelAndView exportXls(HttpServletRequest request, MesPartTagCfg mesPartTagCfg) {
        return super.exportXls(request, mesPartTagCfg, MesPartTagCfg.class, "自定义参数");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("mes_part_tag_cfg:importExcel")
    @DS("#header.line")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MesPartTagCfg.class);
    }

}
